<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$army_id = getPostValue( 'army_id' );
$query = "SELECT ruler_id, name, training, x, y, mission, fortified, follow " .
		"FROM army " .
		"WHERE army_id = '" . $army_id . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$query2 = "SELECT ruler_id " .
		"FROM village " .
		"JOIN village_buildings ON village.village_id = village_buildings.village_id " .
		"WHERE x = " . $row[ 'x' ] . " AND y = " . $row[ 'y' ] . " AND building_id = 22";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	if( $row[ 'follow' ] > 0 || $row[ 'training' ] > 0 || $row[ 'fortified' ] > 0 || $row[ 'mission' ] > 0 || $row[ 'travel' ] > 0 && ( !isset( $row2[ 'ruler_id' ] ) || ( isset( $row2[ 'ruler_id' ] ) && $row2[ 'ruler_id' ] != $_SESSION[ 'ruler_id' ] ) ) ) {
		$status = "This army can't train soldiers right now.";
	}
	else {
		$name = $row[ 'name' ];
		mysql_free_result( $result );
		if( $_POST[ 'scout' ] != "" && $_POST[ 'scout' ] > 0 ) {
			$scout = getPostValue( 'scout' );
		}
		else {
			$scout = 0;
		}
		if( $_POST[ 'infantry' ] != "" && $_POST[ 'infantry' ] > 0 ) {
			$infantry = getPostValue( 'infantry' );
		}
		else {
			$infantry = 0;
		}
		if( isset( $_POST[ 'cavalry' ] ) && $_POST[ 'cavalry' ] > 0 ) {
			$cavalry = getPostValue( 'cavalry' );
		}
		else {
			$cavalry = 0;
		}
		if( isset( $_POST[ 'artillery' ] ) && $_POST[ 'artillery' ] > 0 ) {
			$artillery = getPostValue( 'artillery' );
		}
		else {
			$artillery = 0;
		}
		
		if( isset( $_POST[ 'calc' ] ) ) {
			$gold_cost = $scout * 3 + $infantry + $cavalry * 10 + $artillery * 6;
			$iron_cost = $infantry * 6 + $cavalry * 36 + $artillery * 48;
			$coal_cost = $infantry * 9 + $cavalry * 24 + $artillery * 96;
			$_SESSION[ 'train_scouts' ] = $scout;
			$_SESSION[ 'train_infantry' ] = $infantry;
			$_SESSION[ 'train_cavalry' ] = $cavalry;
			$_SESSION[ 'train_artillery' ] = $artillery;
			
			$status = '<br />' . translate( "Required resources" ) . ":<br />" .
					translate( "Gold" ) . ": <strong class=\"gold\">" . number_format( $gold_cost ) . "</strong><br />" .
					translate( "Iron" ) . ": <strong class=\"iron\">" . number_format( $iron_cost ) . "</strong><br />" .
					translate( "Coal" ) . ": <strong class=\"coal\">" . number_format( $coal_cost ) . "</strong><br />";
		}
		else {
			$query = "SELECT recruits, gold, coal, iron " .
					"FROM ruler " .
					"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			$recruits = $row[ 'recruits' ];
			$gold = $row[ 'gold' ];
			$iron = $row[ 'iron' ];
			$coal = $row[ 'coal' ];
		
			if( $scout + $infantry + $cavalry + $artillery < 1 ) {
				$status = translate( "Please, specify how many units you want to train!" );
			}
			else {
				$query = "SELECT * " .
						"FROM army " .
						"WHERE army_id = " . $army_id;
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		
				$size = $row[ 'scout' ] + $row[ 'infantry' ] + $row[ 'cavalry' ] + $row[ 'artillery' ];
		
				$query = "SELECT rank " .
						"FROM officers " .
						"WHERE officer_id = " . $row[ 'officer_id' ];
				$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
				if( isset( $row2[ 'rank' ] ) ) {
					$maxsize = 50;
					for( $i = 0; $i < $row2[ 'rank' ]; $i ++ ) {
						$maxsize *= 5;
					}
				}
				else {
					$maxsize = 10;
				}
				
				$addedStatus = increaseRankOfficer( $row[ 'officer_id' ], ( $size + $scout + $infantry + $cavalry + $artillery ) );
				if( $addedStatus != '' ) {
					$addedStatus = '<br />' . translate( $addedStatus );
				}
				
				if( $size >= $maxsize ) {
					$status = translate( "The army is already at maximum size." ) . $addedStatus;
					$_SESSION[ 'train_scouts' ] = $scout;
					$_SESSION[ 'train_infantry' ] = $infantry;
					$_SESSION[ 'train_cavalry' ] = $cavalry;
					$_SESSION[ 'train_artillery' ] = $artillery;
				}
				else {
					$continue = true;
					if( $size + $scout > $maxsize ) {
						$status = translate( "Can't add all, army reached it's maximum size." . $addedStatus );
						$continue = false;
						/*
						$scout = $maxsize - $size;
						$infantry = 0;
						$cavalry = 0;
						$artillery = 0;
						*/
					}
					else if( $size + $scout + $infantry > $maxsize ) {
						$status = translate( "Can't add all, army reached it's maximum size." . $addedStatus );
						$continue = false;
						/*$infantry = $maxsize - ( $size + $scout );
						$cavalry = 0;
						$artillery = 0;*/
					}
					else if( $size + $scout + $infantry + $cavalry > $maxsize ) {
						$status = translate( "Can't add all, army reached it's maximum size." . $addedStatus );
						$continue = false;
						/*$cavalry = $maxsize - ( $size + $scout + $infantry );
						$artillery = 0;*/
					}
					else if( $size + $scout + $infantry + $cavalry + $artillery > $maxsize ) {
						$status = translate( "Can't add all, army reached it's maximum size." . $addedStatus );
						$continue = false;
						//$artillery = $maxsize - ( $size + $scout + $infantry + $cavalry );
					}
					else {
						$status = translate( "Training started." );
					}
		
					if( $recruits < $scout ) {
						$status = translate( "You don't have enough recruits." );
						$continue = false;
						
						/*$scout = $recruits;
						$infantry = 0;
						$cavalry = 0;
						$artillery = 0;*/
					}
					else if ( $recruits < $scout + $infantry ) {
						$status = translate( "You don't have enough recruits." );
						$continue = false;
						/*
						$infantry = $recruits - $scout;
						$cavalry = 0;
						$artillery = 0;*/
					}
					else if ( $recruits < $scout + $infantry + $cavalry ) {
						 $status = translate( "You don't have enough recruits." );
						$continue = false;
						/*$cavalry = $recruits - ( $scout + $infantry );
						$artillery = 0;*/
					}
					else if ( $recruits < $scout + $infantry + $cavalry + $artillery ) {
						$status = translate( "You don't have enough recruits." );
						$continue = false;
						//$artillery = $recruits - ( $scout + $infantry + $cavalry );
					}
		
					$gold_cost = $scout * 3 + $infantry + $cavalry * 10 + $artillery * 6;
					$iron_cost = $infantry * 6 + $cavalry * 36 + $artillery * 48;
					$coal_cost = $infantry * 9 + $cavalry * 24 + $artillery * 96;
		
					if( $gold_cost > $gold ) {
						$status = translate( "You don't have enough gold." ) . " (" . translate( "required gold" ) . ": <strong class=\"gold\">" . number_format( $gold_cost ) . "</strong>)";
						$continue = false;
						
					}
					else if( $coal_cost > $coal ) {
						$status = translate( "You don't have enough coal." ) . " (" . translate( "required coal" ) . ": <strong class=\"coal\">" . number_format( $coal_cost ) . "</strong>)";
						$continue = false;
					}
					else if( $iron_cost > $iron ) {
						$status = translate( "You don't have enough iron." ) . " (" . translate( "required iron" ) . ": <strong class=\"iron\">" . number_format( $iron_cost ) . "</strong>)";
						$continue = false;
					}
					else if( $gold_cost == 0 && $iron_cost == 0 && $coal_cost == 0 ) {
						$status = translate( "Nothing to train." );
						$continue = false;
					}
					
					if( $continue ) {
						$time = 16;
						/*if( $artillery > 0 ) {
							$time = 20;
						}
						else if( $cavalry > 0 ) {
							$time = 20;
						}
						else if( $infantry > 0 ) {
							$time = 15;
						}
						else {
							$time = 14;
						}*/
						
						//echo $scout . "<br>";
						//echo $infantry . "<br>";
						//echo $cavalry . "<br>";
						//echo $artillery . "<br>";
						$current_turn = getCurrentTurn();
						
						$query = "INSERT INTO transfer ( scouts, infantry, cavalry, artillery, to_army_id, complete_turn, ruler_id ) " .
								"VALUES ( $scout, $infantry, $cavalry, $artillery, $army_id, " . ( $current_turn + $time ) . ", " . $_SESSION[ 'ruler_id' ] . " )";
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	
						$query = "UPDATE ruler " .
								"SET recruits = " . ( $recruits - ( $scout + $infantry + $cavalry + $artillery ) ) . ", " .
										"gold = " . ( $gold - $gold_cost ) . ", " .
										"iron = " . ( $iron - $iron_cost ) . ", " .
										"coal = " . ( $coal - $coal_cost ) . ", 
										scouts = scouts + $scout, 
										infantry = infantry + $infantry, 
										cavalry = cavalry + $cavalry, 
										artillery = artillery + $artillery " . 
								"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		
						$query = "UPDATE army SET training = 1 WHERE army_id = " . $army_id;
						mysql_query( $query ) or die( "Query failed : " . mysql_error() );
					}
					else {
						$_SESSION[ 'train_scouts' ] = $scout;
						$_SESSION[ 'train_infantry' ] = $infantry;
						$_SESSION[ 'train_cavalry' ] = $cavalry;
						$_SESSION[ 'train_artillery' ] = $artillery;
					}
				}
			}
		}
	}

	$_SESSION[ 'add_army_status' ] = $status;
	header( "Location: army.php?name=$name" );
}
else {
?>
<HTML>
	<HEAD>
		<TITLE>Ages of Strife: <?php transprint( 'Access denied' ); ?></TITLE>
		<link rel="stylesheet" href="../global/agesofstrife.css">
	</HEAD>
	<BODY>
		<div id="container">
			<?php include( "../global/tabs.php" ); ?>
			<div id="main-content">
				<?php transprint( 'You don\'t own this army!' ); ?><br />
			</div>
		</div>
	</BODY>
</HTML>
<?php
}
?>
